package com.tj.tj_java.service.impl;

import com.tj.tj_java.mapper.FavoriteMapper;
import com.tj.tj_java.pojo.Music;
import com.tj.tj_java.result.Result;
import com.tj.tj_java.service.FavoriteService;
import com.tj.tj_java.vo.MusicVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FavoriteServiceImpl implements FavoriteService {

    @Autowired
    private FavoriteMapper favoriteMapper;

    //获取用户收藏列表 包含模糊查询
    @Override
    public Result getUserFavoriteMusicList(int userId,String musicTitle) {
        //通过 查询 获取该用户收藏的全歌曲
        List<Music> Musics = favoriteMapper.getUserFavoriteMusicList(userId,musicTitle);
        //创建要返回给前端的收藏数组
        List<MusicVo> resultList = new ArrayList<>();
        //遍历该用户的歌曲给每一首收藏的歌添加star状态
        for (Music music : Musics) {
            MusicVo musicVo = MusicVo.builder()
                    .musicId(music.getMusicId())
                    .musicTitle(music.getMusicTitle())
                    .imgAddress(music.getImgAddress())
                    .singer(music.getSinger())
                    .Star(true)
                    .build();
            resultList.add(musicVo);
        }
        return Result.success("成功获取用户收藏夹",resultList);
    }
    //添加收藏
    @Override
    public Result addFavorite(int id, int musicId) {
        favoriteMapper.addFavorite(id,musicId);
        return Result.success("收藏成功");
    }
    //取消收藏
    @Override
    public Result removeFavorite(int id, int musicId) {
        favoriteMapper.removeFavorite(id,musicId);
        return Result.success("取消收藏成功");
    }
}
